package com.example.senior_config.initialzer;

import com.example.senior_config.config.MyConfig;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * @description 替代web.xml(web应用初始值)
 * @author ljw
 * @date 2021/9/8 7:35
 */
public class WebInitialzer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        //注册配置类
        ctx.register(MyConfig.class);
        //设置上下文
        ctx.setServletContext(servletContext);
        //设置上下文跳转核心
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        //设置映射路径
        dispatcher.addMapping("/");
        //设置启动顺序
        dispatcher.setLoadOnStartup(1);
        //开启异步方法支持
        dispatcher.setAsyncSupported(true);
    }
}
